#!/usr/bin/env python
# -*- coding:utf-8 -*-

"""
@author zyx
@since 2022/2/11 23:32
@file: c06_防盗链.py
"""

import requests
from lxml import etree

headers = {
    'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) '
                  'Chrome/97.0.4692.71 Safari/537.36',
    "Referer": "http://blog.sina.com.cn/",

}

url = 'http://blog.sina.com.cn/s/blog_01ebcb8a0102zi7r.html?tj=1'
page_text = requests.get(url=url, headers=headers).text
tree = etree.HTML(page_text)
img_src = tree.xpath('//*[@id="sina_keyword_ad_area2"]/div/a/img/@real_src')
for src in img_src:
    fn = src.split('/')[-1]
    data = requests.get(src, headers=headers).content
    with open(f'./{fn}.jpg', 'wb') as fp:
        fp.write(data)
        print(f'{fn}下载成功')
